<template>
  <div>
    <el-card class="box-card">
      <!-- 头部搜索 -->
      <el-row class="searchHeade">
        <el-col :span="18">
          <el-col :span="7" class="keyword">
            <span>关键字</span><el-input v-model="queryData.keyword" placeholder="根据文章标题搜索"></el-input>
          </el-col>
          <el-col :span="7">
            <span style="margin-right:20px">状态</span>
            <el-select v-model="queryData.state" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-col>
            <el-button @click="clearAll">清除</el-button>
            <el-button @click="searchArticle" type="primary">搜索</el-button>
        </el-col>
        <el-col :span="6" class="addSkill">
          <el-button @click="addSkillshow" type="success" icon="el-icon-edit">新增技巧</el-button>
        </el-col>
      </el-row>
      <!-- alert警告 -->
      <el-alert
        class="alertinfo"
        type="info"
        show-icon
        :closable="false">
        <template slot="title">
          共有{{ total }}条数据
        </template>
      </el-alert>
      
      <!-- 表格部分 -->
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" width="100px"></el-table-column>
        <el-table-column label="文章标题" >
          <template slot-scope="scope">
            <span>{{scope.row.title}}</span>
            <a href="#" v-if="scope.row.videoURL" class="el-icon-film" @click="videoShow"></a>
            <div class="videoall" v-if="show">
              <div class="close" @click="closeVideo">
                <span class="el-icon-close"></span>
              </div>
              <!-- 视频弹层 -->
              <div class="videomasks" v-if="scope.row.videoURL">
                <video :src='scope.row.videoURL' autoplay></video>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"></el-table-column>
        <el-table-column prop="username" label="录入人"></el-table-column>
        <el-table-column prop="createTime" label="录入时间">
          <template slot-scope="scope">{{ scope.row.createTime |  parseTimeByString}}</template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state===1">已启用</span>
            <span v-else>已禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text">预览</el-button>
            <el-button @click="isStart(scope.row)" v-if="scope.row.state===0" type="text">启用</el-button>
            <el-button @click="isStart(scope.row)" v-else type="text">禁用</el-button>
            <el-button @click="editArticles(scope.row)" type="text" :disabled="scope.row.state===1?true:false">修改</el-button>
            <el-button @click="deleteArticles(scope.row)" type="text" :disabled="scope.row.state===1?true:false">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页部分 -->
      <el-pagination
        class="fenye"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.page"
        :page-sizes="[2, 5, 8, 10]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!-- 新增技巧对话框 -->
    <el-dialog
      title="新增文章"
      :visible.sync="addArticlesdialogVisible"
      width="50%">
      <!-- 表单区域 -->
      <el-form :rules="addrules" ref="addform" :model="addSkillform" label-width="100px">
        <el-form-item label="文章标题:" prop="title">
          <el-input v-model="addSkillform.title" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章内容:" prop="articleBody">
          <quill-editor ref="myTextEditor" v-model="addSkillform.articleBody" :options="editorOption"  style="height:200px;"></quill-editor>
        </el-form-item>
        <el-form-item class="videoAdderss" label="视频地址:" prop="videoURL">
          <el-input v-model="addSkillform.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addArticlesdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSkillcommit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改技巧对话框 -->
    <el-dialog
      title="修改文章"
      :visible.sync="editArticlesdialogVisible"
      width="50%">
      <!-- 表单区域 -->
      <el-form :rules="editrules" ref="editform" :model="editSkillform" label-width="100px">
        <el-form-item label="文章标题:" prop="title">
          <el-input v-model="editSkillform.title" placeholder="请输入文章标签">
          </el-input>
        </el-form-item>
        <el-form-item label="文章内容:" prop="articleBody">
          <quill-editor ref="myTextEditor" v-model="editSkillform.articleBody" :options="editorOption"  style="height:200px;"></quill-editor>
        </el-form-item>
        <el-form-item class="videoAdderss" label="视频地址:" prop="videoURL">
          <el-input v-model="editSkillform.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editArticlesdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSkillcommit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, changeState, add, remove } from '@/api/hmmm/articles.js'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  data () {
    return {
      queryData: {
        keyword: '',
        page: 1,
        pagesize: 8
      },
      options : [{
        value: '1',
        label: '启用'
      }, {
        value: '0',
        label: '禁用'
      }],
      value: '',
      list: [],
      total:0,
      addArticlesdialogVisible: false,
      editArticlesdialogVisible: false,
      addSkillform: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      editSkillform: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      title: '',
      addrules: {
        title: 
          [
            { required: true, message: '请输入文章标签', trigger: 'blur' }
          ],
        articleBody:
         [
            { required: true, message: '请输入文章内容', trigger: 'blur' }
         ]
      },
      editrules: {
        title: 
          [
            { required: true, message: '请输入文章标签', trigger: 'blur' }
          ],
        articleBody:
         [
            { required: true, message: '请输入文章内容', trigger: 'blur' }
         ]
      },
      
       editorOption:{
        modules:{
          toolbar:[
            ['bold', 'italic', 'underline', 'strike'],      // toggled buttons
            [{'list': 'ordered'}, {'list': 'bullet'}],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        },
        placeholder: ''
      },
      show: false
    }
  },
  components: {
    quillEditor
  },
  created () {
    this.getArticlesList()
  },

  methods: {
    // 获取文章列表
    async getArticlesList () {
      const { data: res } = await list(this.queryData)
      this.list = res.items
      this.total = res.counts
      console.log(res)
    },
    // 点击搜索
    searchArticle() {
        this.getArticlesList()
    },
    // 点击清除
    clearAll() {
      this.queryData.keyword = '',
      this.queryData.state = null
      this.getArticlesList()
    },
    // 页码发生变化
    handleCurrentChange(val) {
      this.queryData.page = val
      this.getArticlesList()
    },
    // 每页条数发生变化
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.getArticlesList()
    },
    // 启用/禁用
    async isStart(row) {
      // console.log(row)
      row.state = row.state?0:1
      const res = await changeState(row)
      if (res.status !== 200) return this.$message.error('操作失败')
      this.$message.success('操作成功')
    },
    // 新增技巧显示
    addSkillshow() {
      this.addArticlesdialogVisible = true
    },
    // 新增确认按钮
    async addSkillcommit() {
      try {
        const res = await add(this.addSkillform)
        console.log(res)
        if(res.status !== 200) {
          this.$message.error('新增失败')
        }
        this.$message.success('添加成功')
        this.addArticlesdialogVisible = false
        this.getArticlesList()
        this.$refs.addform.resetFields()
      } catch (error) {
        this.$messag.error('新增失败，请稍后重试')
      }
    },
    // 编辑技巧显示
    editArticles(row) {
      this.editArticlesdialogVisible = true
      console.log(row)
      this.editSkillform.title = row.title
      this.editSkillform.articleBody = row.articleBody
      this.editSkillform.videoURL = row.videoURL
    },
    // 编辑确认按钮
    editSkillcommit() {

    },
    // 删除文章
    async deleteArticles(row) {
      try {
        const res = await remove(row)
        console.log(res)
        if(res.status !== 200) {
          this.$message.error('删除失败')
        }
        this.$message.error('删除成功')
        this.getArticlesList()
        
      } catch (error) {
         this.$message.error('删除失败，请稍后重试')
      }
    },
    //视频弹层
    videoShow () {
      this.show = true
    },
    closeVideo () {
      console.log(1);
      this.show = false
    }
  }
}
</script>

<style scoped lang='less'>
/deep/.box-card {
  margin: 15px 10px;
  .keyword {
    display: flex;
    span {
      min-width: 60px;
      line-height: 36px;
    }
  }
  .addSkill {
    display: flex;
    justify-content:flex-end
  }
  .searchHeade {
    margin-bottom: 20px;
    .el-input__inner {
    width: 260px;
  }
  }
  .alertinfo {
    margin-bottom: 20px;
  }
  .fenye {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
  .el-icon-film {
    color: #00f;
    margin-left: 5px;
  }
  .videomasks {
    width: 800px;
    height: 600px;
    video {
      width: 100%;
      height: 100%;
    }
  }
  .videoall{
	position:fixed;
	top:50%;
  left:50%;
  transform: translate(-50%,-50%);
  z-index:10;
  width: 800px;
  height: 600px;
  background-color: rgba(255, 255, 255, 0);
  .close {
    width: 50px;
    height: 50px;
    background: rgba(0,0,0,.1);
    position: absolute;
    top: 10px;
    left: 45%;
    line-height: 55px;
    text-align: center;
    border-radius: 50%;
    z-index: 9999;
    .el-icon-close{
      color: #fff;
      font-size: 20px;
    }
  }
  }
}
.videoAdderss {
  margin-top: 70px;
}
</style>
